package com.software.seakdream.controller;

import com.software.seakdream.domain.Result;
import com.software.seakdream.entity.AudioInfo;
import com.software.seakdream.entity.OfficialRecommendation;
import com.software.seakdream.entity.Recommend;
import com.software.seakdream.entity.RecommendAudio;
import com.software.seakdream.service.AudioInfoService;
import com.software.seakdream.service.RecommendService;
import com.software.seakdream.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private AudioInfoService audioInfoService;
    public static final String WHITE_NOISE = "白噪音";
    public static final String CROSS_TALK = "相声";
    public static final String BEDTIME_STORY = "睡前故事";
    public static final String OPERA = "戏曲";
    public static final String MUSE = "冥想";
    public static final String LULLABY= "摇篮曲";
    public static final String ASMR = "ASMR";
    public static final String LIGHT_MUSIT = "轻音乐";

    @GetMapping("/getRecommendData")
    public Result getRecommendData(String userId, String type){
        List<RecommendAudio> recommends = new ArrayList<RecommendAudio>();
        RecommendAudio recommendAudio;
        AudioInfo audioInfo;
        System.out.println(type);
        if(type.equals(WHITE_NOISE)) {
            List<OfficialRecommendation> officialRecommendations = recommendService.getWhiteRecommendation();
            for (OfficialRecommendation temp : officialRecommendations) {
                RecommendAudio audio = new RecommendAudio();
                audio.setTitle(temp.getMusicName());
                audio.setTime(temp.getMusicTime());
                audio.setImageUrl(temp.getMusicImage());
                audio.setWhite(true);
                audio.setWhiteId1(temp.getSelectedId1());
                audio.setWhiteId2(temp.getSelectedId2());
                audio.setWhiteId3(temp.getSelectedId3());
                int count = 0;
                if (audio.getWhiteId1() != null && !audio.getWhiteId1().equals("")) {
                    audioInfo = audioInfoService.findAudioInfoById(audio.getWhiteId1());
                    audio.setWhiteImg1(audioInfo.getAudioPic());
                    audio.setWhiteName1(audioInfo.getAudioName());
                    audio.setWhiteUrl1(audioInfo.getAudioUrl());
                    count++;
                }
                if (audio.getWhiteId2() != null && !audio.getWhiteId2().equals("")) {
                    audioInfo = audioInfoService.findAudioInfoById(audio.getWhiteId2());
                    audio.setWhiteImg2(audioInfo.getAudioPic());
                    audio.setWhiteName2(audioInfo.getAudioName());
                    audio.setWhiteUrl2(audioInfo.getAudioUrl());
                    count++;
                }
                if (audio.getWhiteId3() != null && !audio.getWhiteId3().equals("")) {
                    audioInfo = audioInfoService.findAudioInfoById(audio.getWhiteId3());
                    audio.setWhiteImg3(audioInfo.getAudioPic());
                    audio.setWhiteName3(audioInfo.getAudioName());
                    audio.setWhiteUrl3(audioInfo.getAudioUrl());
                    count++;
                }
                audio.setWhiteCount(count);
                recommends.add(audio);
            }
        }else{
            List<Recommend> recommendList = recommendService.getRecommendListByType(type);
            for(Recommend temp:recommendList){
                RecommendAudio audio = new RecommendAudio();
                System.out.println(temp.toString());
                audio.setTitle(temp.getName());
                audio.setTime(temp.getTime());
                audio.setImageUrl(temp.getImg());
                audio.setAudioId(temp.getAudioId());
                audioInfo = audioInfoService.findAudioInfoById(audio.getAudioId());
                audio.setAudioUrl(audioInfo.getAudioUrl());
                audio.setWhite(false);
                recommends.add(audio);
            }
        }

        return ResultUtils.success(recommends);

    }
}
